package com.fhz.kotlintest.databinding.adapter

import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.fhz.kotlintest.Data
import com.fhz.kotlintest.databinding.RvDatabindingLayoutBinding
import org.jetbrains.annotations.NotNull
import androidx.databinding.BindingAdapter
import com.bumptech.glide.request.target.Target


/**
 * 时间:2022/1/5
 * @author msfeng
 * 简述: 万能适配器
 */
class RVDataBindingAdapter(layoutResId: Int,list:MutableList<Data>)
    : BaseQuickAdapter<Data, BaseDataBindingHolder<RvDatabindingLayoutBinding>>(layoutResId,list){

    override fun convert(@NotNull holder: BaseDataBindingHolder<RvDatabindingLayoutBinding>, item: Data) {
        // 获取 Binding
        var binding = holder.dataBinding
        binding?.let {
            binding.food = item
            //中划线
            binding.rvPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
            //防治闪烁
            binding.executePendingBindings()
        }
    }

    //注意加入    id 'kotlin-kapt' 插件
    companion object{
        @BindingAdapter("url")
        @JvmStatic
        fun bindImgUrl(imageView: ImageView, url: String) {
            Glide.with(imageView.context).load(url).into(imageView)
        }
    }


    /**
     * 当 ViewHolder 创建完毕以后，会执行此回掉
     * 可以在这里做任何你想做的事情
     */
    override fun onItemViewHolderCreated(
        viewHolder: BaseDataBindingHolder<RvDatabindingLayoutBinding>,
        viewType: Int
    ) {
        // 绑定 view
//        DataBindingUtil.bind<RvDatabindingLayoutBinding>(viewHolder.itemView)
    }

}